define(function(require, exports, module) {
    var router = require("router");
    //var app = require("app");
    var controller = {};
    var goodslist={};
    var goods_spec={};
    controller.vue = function() {
        controller.vm = new Vue({
            router: router,
            data: function() {
                return {
                       shop_info:{},
                      shoppingCartList:[],
                      goods:{},
                      tabid:-1,
                      popup:0,
                      popup2:0,
                      data:{},
                      choose_property_data: {},
                      choose_spec: {},
                      disabled: 0,
                      spec_keys_str: '',
                      goods_in_cart:{},
                      tag_list:[]
                };
            },
            el: '#pageGoods',
            created: function() {
                this.fetchData();
            },
            
            methods: {
                fetchData: function() {
                    var that = this;
                    this.shop_id=this.$route.params.shop_id;
                    var user_info = app.getUserInfo();
                    if( this.shop_id >0){
                      this.getShopInfo();
                      this.getTagList();
                    }else{
                      
                      app.component("get_shop_id",{
                          success:function(shop_id){
                              that.shop_id = shop_id;
                              that.$router.push({name:"goods",params:{shop_id:shop_id}});
                          }
                       });
                    }
                    
                },
                 setData:function(arr){
                  for( var i in arr){
                    // console.log(i);
                    // console.log(arr[i]);
                     // this.$set(i,arr[i]); 
                     this[i] = arr[i];
                  }
                },
                // 商铺信息
                getShopInfo:function(){
                  var that = this;
                  app.get("mall/shop/find", { shop_id: this.shop_id }, function (res) {
                    if (res.code != 200) {
                      app.alert(res.msg);
                    } else if (res.data) {
                       that.shop_info = res.data;
                    }
                  });
                },

                
                // 标签列表
                getTagList:function(){
                  var that = this;
                  app.get("mall/tag/get", { shop_id: this.shop_id }, function (res) {
                    if (res.code != 200) {
                      app.alert(res.msg);
                    }else if (res.data) {
                       that.tag_list = res.data.list;
                       that.showTabs(0);
                    }
                  });
                },
               
              //商品列表
                loadList:function(reload){
                  var that = this;
                  var attr_id = this.attr_id;
                  if( that.goods[attr_id] && that.goods[attr_id].length>0){
                     return ;
                  }
                  app.get("mall/goods/get", {
                      attr_id:this.attr_id
                      // status:1
                    },  (res)=> {
                      if (res.code != 200) {
                        app.alert(res.msg);
                      } 
                      
                      if (res.data && res.data.list ){
                       goodslist[attr_id] = res.data.list;
                      }else{
                       goodslist[attr_id] = [];
                      }
                      var that_goods=that.goods;
                      var goods=[];
                      for (var k in goodslist[attr_id]){
                        var item = goodslist[attr_id][k];
                        goods.push({
                          default_img: item['default_img'],
                          goods_name: item['goods_name'],
                          origin_price: item['origin_price'],
                          price: item['price'],
                          total_stock: item['total_stock'],
                          sub_title: item['sub_title'],
                          id:item['id'],
                          status:item['status'],
                        });
                      }
                      that.goods={};
                      that_goods[attr_id] = goods;
                      that.goods = that_goods;
                      // console.log(that.goods);
              
                    });
                },
                showTabs:function(index){
  
                  if (this.tabid == index) {
                    return;
                  }

                  var tag =  this.tag_list[index];
                  // console.log(tag);
                  this.attr_id = tag.id;
                  this.setData({ tabid: index });
                  this.loadList();
                   
                },
                deleteRowMallGoods:function(attr_id,idx2,id){
                  // console.log(idx2);
                  var that = this;
                  var goods = this.goods;
                   app.get('/mall-admin/mall-goods/delete', {
                     shop_id:this.shop_id,
                     ids: id
                  }, function(data) {
                      if (data.error === 0) {
                           var status = goods[attr_id][idx2].status ;
                           goods[attr_id][idx2].status = (status==1?2:1);
                           // console.log(status);
                           //  console.log(goods);
                             that.goods=[];
                           that.goods = goods;
                      } else {
                          app.alert(data.msg);
                      }
                  });
                },
                editGoods:function(id){
                  var user_info = app.getUserInfo();
                  var url ='https://www.bestsmartbee.com/mall-admin/mall-goods/add?id='+id+"&token="+user_info.token+"&shop_id="+this.shop_id;
                  // console.log(url);
                  window.location.href=url;
                  
                },
                 newGoods:function(id){
                  var user_info = app.getUserInfo();
                  var url ='https://www.bestsmartbee.com/mall-admin/mall-goods/add?shop_id='+this.shop_id+'&token='+user_info.token;
                  // console.log(url);
                  window.location.href=url;
                  
                },

               
                 
           
               
                
        }
            

        }).$mount('#app');
    };
    module.exports = controller;
});
